let app = getApp();
// import utility from "./utility.js";
const appUrl = "https://wx.nongducheng.com/";
//const appApiUrl = appUrl + "api/";
// 文件后台地址
const fsApiUrl = "http://116.62.58.28/zeyy_fs/api/";

// 移动云
// APP后台地址
// const appUrl = "http://36.137.233.32:9085/AppServer/"
const appApiUrl = appUrl + "api/";
// 对Url Query参数进行编码
function encodeQuery(obj) {
  var arr = [];
  if (obj) {
    for (var key in obj) {
      if (obj.hasOwnProperty(key)) {
        var value = obj[key];
        if (value != null)
          arr.push(encodeURIComponent(key) + "=" + encodeURIComponent(value));
      }
    }
  }
  return arr.join("&");
}

// get异步请求封装
function get(url) {
  var promise = new Promise((resolve, reject) => {
    dd.httpRequest({
      url: url,
      method: "GET",
      dataType: "json",
      // header: {
      // 	'Token': utility.getToken()
      // },
      success: function(res) {
         
        if (res.status === 200) resolve(res.data);
        else if (res.status == 401)
          dd.reLaunch({
            url: "/pages/login/login?reason=1",
            animationDuration: 450
          });
        else {
          
          dd.showToast({
            title: "服务器请求失败"
          });
        }
      },
      fail: function(e) {
        dd.showToast({
          title: "服务器请求失败"
        });
      }
    });
  });
  return promise;
}

// post异步请求封装
function post(url, data) {
  var promise = new Promise((resolve, reject) => {
    dd.httpRequest({
      url: url,
      method: "POST",
      data: data,
      dataType: "json",
      // header: {
      // 	'Content-Type': 'application/json',
      // 	'Token': utility.getToken()
      // },
      success: function(res) {
      
        if (res.status === 200) resolve(res.data);
        else if (res.status == 401)
          dd.reLaunch({
            url: "/pages/login/login?reason=1",
            animationDuration: 450
          });
        else {
          console.log(res, url);
          dd.showToast({
            title: "服务器请求失败"
          });
        }
      },
      fail: function(e) {
        dd.showToast({
          title: "服务器请求失败"
        });
      }
    });
  });
  return promise;
}
let http = {
  Token: {
    Get: e =>
      get(
        "https://oapi.dingtalk.com/gettoken?appkey=ding0etja9mayg7zjvog&appsecret=nPH7Ag8WnYDqOa3ZwEMrkthJhsQo0SjzaSHoJjJ5QTLr4CWThOfIMweMH47obWbO" +
          encodeQuery(e)
      )
  },
  Department: {
    //access_token+        //data:dept_id:1
    Getlist: e =>
      post(
        "https://oapi.dingtalk.com/topapi/v2/department/listsubid?" +
          encodeQuery(e)
      )
  },
  User: {
    //access_token+     data:dept_id+language: "zh_CN"
    Getlist: e =>
      post("https://oapi.dingtalk.com/topapi/user/listid?" + encodeQuery(e)),
    //access_token+     data:userid
    Getuserdetail: e =>
      post("https://oapi.dingtalk.com/topapi/v2/user/get?" + encodeQuery(e)),
    //access_token+code
    Getmydetail: e =>
      post(
        "https://oapi.dingtalk.com/topapi/v2/user/getuserinfo?" + encodeQuery(e)
      )
  },
  EST: {
    User: {
      Get: e =>
        get(
          appApiUrl +
            "wx/Bill/GetPagedList?pageIndex=1&pageSize=11/?" +
            encodeQuery(e)
        ),
      Post: e => get("https://oapi.dingtalk.com/gettoken?" + encodeQuery(e)),
      Getlist: e =>
        post(
          "ttps://oapi.dingtalk.com/topapi/v2/department/listsubid?" +
            encodeQuery(e)
        )
    }
  }
};
module.exports = http;
